package com.cap.bts.crmdp.dealer.meal.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.meal.dao.IPackageEligibilityDao;
import com.cap.bts.crmdp.dealer.meal.domain.PackageEligibilityModel;
import com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;

/**
 * 套餐的适用范围
 * <p>Title: PackageEligibilityDaoImpl.java</p>
 * @author lizhiyong
 * @date 2015-11-4 下午2:08:34 
 * @version v 0.1
 */
@Repository("iPackageEligibilityDao")
public class PackageEligibilityDaoImpl extends BaseDaoImpl<PackageEligibilityModel, Long> implements
                                                                                         IPackageEligibilityDao {

    @Inject
    public PackageEligibilityDaoImpl(@Value("com.cap.bts.crmdp.dealer.meal.domain.PackageEligibilityModel") Class<PackageEligibilityModel> clazz) {
        super(clazz);

    }

    /**
     * 查询套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.dao.IPackageEligibilityDao#getPackageEligibilityList(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public List<PackageEligibilityModel> getPackageEligibilityList(PackageEligibilityDto packageEligibilityDto)
                                                                                                               throws AppException {
        String selectStr = "select a.id,a.Package_def_id,a.Dealer_property,case when a.Dealer_property is not null then c.code_value else null end code,"
                           + "a.Property_value,a.code_type,a.create_by,a.create_date,a.last_update_by,a.last_update_date from tm_package_eligibility a";

        String leftStr = " left join tc_code_value c on a.code_type=c.code_type and a.Dealer_property=c.code";
        // 查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(" where 1=1 ");
        if (packageEligibilityDto.getPackageDefId() != null) {
            where.append(" and a.Package_def_id='" + packageEligibilityDto.getPackageDefId() + "'");
        }
        String order = " order by a.id desc";

        selectSql.append(selectStr).append(leftStr).append(where.toString()).append(order);

        return super.getJpaTemplate().execute(
            new NativeQueryJpaCallback<PackageEligibilityModel>(selectSql.toString(),
                PackageEligibilityModel.class));
    }

    /**
     * 新增套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.dao.IPackageEligibilityDao#insertPackageEligibilityInfo(com.cap.bts.crmdp.dealer.meal.domain.PackageEligibilityModel)
     */
    @Override
    public PackageEligibilityModel insertPackageEligibilityInfo(PackageEligibilityModel packageEligibilityModel)
                                                                                                                throws AppException {
        return save(packageEligibilityModel);

    }

    /**
     * 删除套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.dao.IPackageEligibilityDao#removePackageEligibilityInfo(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public boolean removePackageEligibilityInfo(PackageEligibilityDto packageEligibilityDto)
                                                                                            throws AppException {
        remove(packageEligibilityDto.getId());
        return true;

    }

    /**
     * 根据套餐ID,Dealer_property,Property_value查询套餐的适用范围
     * @see com.cap.bts.crmdp.dealer.meal.dao.IPackageEligibilityDao#getPackageEligibilityInfoByPackageDefId(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public PackageEligibilityModel getPackageEligibilityInfoByPackageDefId(PackageEligibilityDto dto)
                                                                                                     throws AppException {
        String selectStr = "select a.id,a.Package_def_id,a.Dealer_property,case when a.Dealer_property is not null then c.code_value else null end code,"
                           + " a.Property_value,a.code_type,a.create_by,a.create_date,a.last_update_by,a.last_update_date from tm_package_eligibility a";

        String leftStr = " left join tc_code_value c on a.code_type=c.code_type and a.Dealer_property=c.code";
        // 查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(" where 1=1 ");
        if (dto.getPackageDefId() != null) {
            where.append(" and a.Package_def_id='" + dto.getPackageDefId() + "'");
        }
        if (dto.getDealerProperty() != null) {
            where.append(" and a.Dealer_property='" + dto.getDealerProperty() + "'");
        }
        if (dto.getPropertyValue() != null) {
            where.append(" and a.Property_value='" + dto.getPropertyValue() + "'");
        }

        selectSql.append(selectStr).append(leftStr).append(where.toString());
        List<PackageEligibilityModel> list = super.getJpaTemplate().execute(
            new NativeQueryJpaCallback<PackageEligibilityModel>(selectSql.toString(),
                PackageEligibilityModel.class));
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;

    }

	@Override
	public List<PackageEligibilityModel> getPackageEligibilityByPackageDefId(Long packageDefId) throws AppException {
		String sql = "select * from tm_package_eligibility where Package_def_id = " + packageDefId;
		return this.getListByNativeQuery(sql);
	}
}
